<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>main.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-graphicsview-boxes-example.html">Boxes</a></td><td >main.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">main.cpp 示例文件</h1>
<span class="subtitle">graphicsview/boxes/main.cpp</span>
<!-- $$$graphicsview/boxes/main.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the demonstration applications of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="comment">//#include &lt;GL/glew.h&gt;</span>
  <span class="preprocessor">#include &quot;glextensions.h&quot;</span>

  <span class="preprocessor">#include &quot;scene.h&quot;</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>
  <span class="preprocessor">#include &lt;QGLWidget&gt;</span>

  <span class="keyword">class</span> GraphicsView : <span class="keyword">public</span> <span class="type"><a href="qgraphicsview.html">QGraphicsView</a></span>
  {
  <span class="keyword">public</span>:
      GraphicsView()
      {
          setWindowTitle(tr(<span class="string">&quot;Boxes&quot;</span>));
          setRenderHints(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing <span class="operator">|</span> <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>SmoothPixmapTransform);
          <span class="comment">//setRenderHints(QPainter::SmoothPixmapTransform);</span>
      }

  <span class="keyword">protected</span>:
      <span class="type">void</span> resizeEvent(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span>event) override {
          <span class="keyword">if</span> (scene())
              scene()<span class="operator">-</span><span class="operator">&gt;</span>setSceneRect(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(<span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span>)<span class="operator">,</span> event<span class="operator">-</span><span class="operator">&gt;</span>size()));
          <span class="type"><a href="qgraphicsview.html">QGraphicsView</a></span><span class="operator">::</span>resizeEvent(event);
      }
  };

  <span class="keyword">inline</span> bool matchString(<span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>extensionString<span class="operator">,</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>subString)
  {
      <span class="type">int</span> subStringLength <span class="operator">=</span> strlen(subString);
      <span class="keyword">return</span> (strncmp(extensionString<span class="operator">,</span> subString<span class="operator">,</span> subStringLength) <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>)
          <span class="operator">&amp;</span><span class="operator">&amp;</span> ((extensionString<span class="operator">[</span>subStringLength<span class="operator">]</span> <span class="operator">=</span><span class="operator">=</span> <span class="char">' '</span>) <span class="operator">|</span><span class="operator">|</span> (extensionString<span class="operator">[</span>subStringLength<span class="operator">]</span> <span class="operator">=</span><span class="operator">=</span> <span class="char">'\0'</span>));
  }

  bool necessaryExtensionsSupported()
  {
      <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>extensionString <span class="operator">=</span> <span class="keyword">reinterpret_cast</span><span class="operator">&lt;</span><span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">&gt;</span>(glGetString(GL_EXTENSIONS));
      <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>p <span class="operator">=</span> extensionString;

      <span class="keyword">const</span> <span class="type">int</span> GL_EXT_FBO <span class="operator">=</span> <span class="number">1</span>;
      <span class="keyword">const</span> <span class="type">int</span> GL_ARB_VS <span class="operator">=</span> <span class="number">2</span>;
      <span class="keyword">const</span> <span class="type">int</span> GL_ARB_FS <span class="operator">=</span> <span class="number">4</span>;
      <span class="keyword">const</span> <span class="type">int</span> GL_ARB_SO <span class="operator">=</span> <span class="number">8</span>;
      <span class="type">int</span> extensions <span class="operator">=</span> <span class="number">0</span>;

      <span class="keyword">while</span> (<span class="operator">*</span>p) {
          <span class="keyword">if</span> (matchString(p<span class="operator">,</span> <span class="string">&quot;GL_EXT_framebuffer_object&quot;</span>))
              extensions <span class="operator">|</span><span class="operator">=</span> GL_EXT_FBO;
          <span class="keyword">else</span> <span class="keyword">if</span> (matchString(p<span class="operator">,</span> <span class="string">&quot;GL_ARB_vertex_shader&quot;</span>))
              extensions <span class="operator">|</span><span class="operator">=</span> GL_ARB_VS;
          <span class="keyword">else</span> <span class="keyword">if</span> (matchString(p<span class="operator">,</span> <span class="string">&quot;GL_ARB_fragment_shader&quot;</span>))
              extensions <span class="operator">|</span><span class="operator">=</span> GL_ARB_FS;
          <span class="keyword">else</span> <span class="keyword">if</span> (matchString(p<span class="operator">,</span> <span class="string">&quot;GL_ARB_shader_objects&quot;</span>))
              extensions <span class="operator">|</span><span class="operator">=</span> GL_ARB_SO;
          <span class="keyword">while</span> ((<span class="operator">*</span>p <span class="operator">!</span><span class="operator">=</span> <span class="char">' '</span>) <span class="operator">&amp;</span><span class="operator">&amp;</span> (<span class="operator">*</span>p <span class="operator">!</span><span class="operator">=</span> <span class="char">'\0'</span>))
              <span class="operator">+</span><span class="operator">+</span>p;
          <span class="keyword">if</span> (<span class="operator">*</span>p <span class="operator">=</span><span class="operator">=</span> <span class="char">' '</span>)
              <span class="operator">+</span><span class="operator">+</span>p;
      }
      <span class="keyword">return</span> (extensions <span class="operator">=</span><span class="operator">=</span> <span class="number">15</span>);
  }

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
  {
      <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

      <span class="keyword">if</span> ((<span class="type">QGLFormat</span><span class="operator">::</span>openGLVersionFlags() <span class="operator">&amp;</span> <span class="type">QGLFormat</span><span class="operator">::</span>OpenGL_Version_1_5) <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>critical(<span class="number">0</span><span class="operator">,</span> <span class="string">&quot;OpenGL features missing&quot;</span><span class="operator">,</span>
              <span class="string">&quot;OpenGL version 1.5 or higher is required to run this demo.\n&quot;</span>
              <span class="string">&quot;The program will now exit.&quot;</span>);
          <span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>;
      }

      <span class="type">int</span> maxTextureSize <span class="operator">=</span> <span class="number">1024</span>;
      <span class="type">QGLWidget</span> <span class="operator">*</span>widget <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QGLWidget</span>(<span class="type">QGLFormat</span>(QGL<span class="operator">::</span>SampleBuffers));
      widget<span class="operator">-</span><span class="operator">&gt;</span>makeCurrent();

      <span class="keyword">if</span> (<span class="operator">!</span>necessaryExtensionsSupported()) {
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>critical(<span class="number">0</span><span class="operator">,</span> <span class="string">&quot;OpenGL features missing&quot;</span><span class="operator">,</span>
              <span class="string">&quot;The OpenGL extensions required to run this demo are missing.\n&quot;</span>
              <span class="string">&quot;The program will now exit.&quot;</span>);
          <span class="keyword">delete</span> widget;
          <span class="keyword">return</span> <span class="operator">-</span><span class="number">2</span>;
      }

      <span class="comment">// Check if all the necessary functions are resolved.</span>
      <span class="keyword">if</span> (<span class="operator">!</span>getGLExtensionFunctions()<span class="operator">.</span>resolve(widget<span class="operator">-</span><span class="operator">&gt;</span>context())) {
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>critical(<span class="number">0</span><span class="operator">,</span> <span class="string">&quot;OpenGL features missing&quot;</span><span class="operator">,</span>
              <span class="string">&quot;Failed to resolve OpenGL functions required to run this demo.\n&quot;</span>
              <span class="string">&quot;The program will now exit.&quot;</span>);
          <span class="keyword">delete</span> widget;
          <span class="keyword">return</span> <span class="operator">-</span><span class="number">3</span>;
      }

      <span class="comment">// TODO: Make conditional for final release</span>
      <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="number">0</span><span class="operator">,</span> <span class="string">&quot;For your information&quot;</span><span class="operator">,</span>
          <span class="string">&quot;This demo can be GPU and CPU intensive and may\n&quot;</span>
          <span class="string">&quot;work poorly or not at all on your system.&quot;</span>);

      widget<span class="operator">-</span><span class="operator">&gt;</span>makeCurrent(); <span class="comment">// The current context must be set before calling Scene's constructor</span>
      Scene scene(<span class="number">1024</span><span class="operator">,</span> <span class="number">768</span><span class="operator">,</span> maxTextureSize);
      GraphicsView view;
      view<span class="operator">.</span>setViewport(widget);
      view<span class="operator">.</span>setViewportUpdateMode(<span class="type"><a href="qgraphicsview.html">QGraphicsView</a></span><span class="operator">::</span>FullViewportUpdate);
      view<span class="operator">.</span>setScene(<span class="operator">&amp;</span>scene);
      view<span class="operator">.</span>show();

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
</div>
<!-- @@@graphicsview/boxes/main.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
